package com.cmnit.analysis.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    //英文简写（默认）：2010-12-01
    public static String FORMAT_SHORT = "yyyy-MM-dd";

    //英文全称：2010-12-01 23:15:06
    public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";

    //精确到毫秒的完整时间：yyyy-MM-dd HH:mm:ss.S
    public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";

    // 中文简写：2010年12月01日
    public static String FORMAT_SHORT_CN = "yyyy年MM月dd";

    // 中文全称：2010年12月01日  23时15分06秒
    public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";

    // 精确到毫秒的完整中文时间
    public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";

    // 小时格式优化
    public static String FORMAT_HOUR = "HH";

    // 天格式优化
    public static String FORMAT_DAY = "dd";

    // 月份格式优化
    public static String FORMAT_MONTH = "MM";

    // 根据字符串获取日期对象
    public static Date getTimeByString(String date) {
        Date time = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat(FORMAT_SHORT);
            time = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    // 获取当前时间
    public static String getTimeString() {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
        Calendar calendar = Calendar.getInstance();
        return df.format(calendar.getTime());
    }

    // 获取日期年份
    public static String getYear(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
        return df.format(date).substring(0, 4);
    }

    // 返回月
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    // 返回日期
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    // 返回小时
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    // 获取指定日期的前后N天
    public static String getDateByAdding(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + num);
        return new SimpleDateFormat(FORMAT_SHORT).format(calendar.getTime());
    }


    // 返回上一天
    public static String getLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 24);
        return new SimpleDateFormat(FORMAT_DAY).format(calendar.getTime());
    }

    // 返回上一天所在的月
    public static String getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 24);
        return new SimpleDateFormat(FORMAT_MONTH).format(calendar.getTime());
    }

    // 返回上一天所在年
    public static String getLastDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 24);
        return calendar.get(Calendar.YEAR) + "";
    }

    // 返回上N个小时
    public static String getLastHour(Date date, Integer hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        new SimpleDateFormat(FORMAT_HOUR).format(calendar.getTime());
        return new SimpleDateFormat(FORMAT_HOUR).format(calendar.getTime());
    }

    // 默认前一小时
    public static String getLastHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        new SimpleDateFormat(FORMAT_HOUR).format(calendar.getTime());
        return new SimpleDateFormat(FORMAT_HOUR).format(calendar.getTime());
    }

    // 返回上N个小时所在年
    public static String getLastHourOfYear(Date date, Integer hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        return calendar.get(Calendar.YEAR) + "";
    }

    // 默认前一小时
    public static String getLastHourOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        return calendar.get(Calendar.YEAR) + "";
    }

    // 返回上N个小时所在月
    public static String getLastHourOfMonth(Date date, Integer hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        return new SimpleDateFormat(FORMAT_MONTH).format(calendar.getTime());
    }

    // 默认前一小时
    public static String getLastHourOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        return new SimpleDateFormat(FORMAT_MONTH).format(calendar.getTime());
    }

    // 返回上N个小时所在日
    public static String getLastHourOfDay(Date date, Integer hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        return new SimpleDateFormat(FORMAT_DAY).format(calendar.getTime());
    }

    // 默认前一小时
    public static String getLastHourOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        return new SimpleDateFormat(FORMAT_DAY).format(calendar.getTime());
    }

    // 返回分
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    // 返回秒钟
    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    // 返回毫秒
    public static long getMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

}
